/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.server.modules.sys.web.home;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.server.common.config.Global;
import com.server.common.persistence.Page;
import com.server.common.web.BaseController;
import com.server.common.utils.StringUtils;
import com.server.modules.sys.entity.home.HomeImg;
import com.server.modules.sys.service.home.HomeImgService;

/**
 * 轮播图管理Controller
 * @author 王磊
 * @version 2016-10-14
 */
@Controller
@RequestMapping(value = "${adminPath}/sys/home/homeImg")
public class HomeImgController extends BaseController {

	@Autowired
	private HomeImgService homeImgService;
	
	@ModelAttribute
	public HomeImg get(@RequestParam(required=false) String id) {
		HomeImg entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = homeImgService.get(id);
		}
		if (entity == null){
			entity = new HomeImg();
		}
		return entity;
	}
	
	@RequiresPermissions("sys:home:homeImg:view")
	@RequestMapping(value = {"list", ""})
	public String list(HomeImg homeImg, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<HomeImg> page = homeImgService.findPage(new Page<HomeImg>(request, response), homeImg); 
		model.addAttribute("page", page);
		model.addAttribute("homeImg", homeImg);
		return "modules/sys/home/homeImgList";
	}

	@RequiresPermissions("sys:home:homeImg:view")
	@RequestMapping(value = "form")
	public String form(HomeImg homeImg, Model model) {
		model.addAttribute("homeImg", homeImg);
		return "modules/sys/home/homeImgForm";
	}

	@RequiresPermissions("sys:home:homeImg:edit")
	@RequestMapping(value = "save")
	public String save(HomeImg homeImg, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, homeImg)){
			return form(homeImg, model);
		}
		String url = StringUtils.getFirstUrl(homeImg.getUrl());//过滤竖线
		homeImg.setUrl(url);
		homeImgService.save(homeImg);
		addMessage(redirectAttributes, "保存轮播图成功");
		return "redirect:"+Global.getAdminPath()+"/sys/home/homeImg/?repage";
	}
	
	@RequiresPermissions("sys:home:homeImg:edit")
	@RequestMapping(value = "delete")
	public String delete(HomeImg homeImg, RedirectAttributes redirectAttributes) {
		homeImgService.delete(homeImg);
		addMessage(redirectAttributes, "删除轮播图成功");
		return "redirect:"+Global.getAdminPath()+"/sys/home/homeImg/?repage";
	}

}